package com.aier.user.entity.userapp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

public class AssignMessageRowMapper implements RowMapper<AssignMessage> {

    @SuppressWarnings("unused")
    private int type;

    public AssignMessageRowMapper() {
    }

    public AssignMessageRowMapper(int type) {
        this.type = type;
    }

    @Override
    public AssignMessage mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseAdminUserInfo(new AssignMessage(), rs);
    }

    public static AssignMessage parseAdminUserInfo(AssignMessage f, ResultSet rs)
        throws SQLException {
        return parseAdminUserInfo(f, rs, null);
    }

    public static AssignMessage parseAdminUserInfo(AssignMessage f, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            f.setAid(rs.getLong(su + "aid"));
        } catch (SQLException e) {
            f.setAid(null);
        }
        try {
            f.setTid(rs.getLong(su + "tid"));
        } catch (SQLException e) {
            f.setTid(null);
        }
        try {
            f.setSid(rs.getLong(su + "sid"));
        } catch (SQLException e) {
            f.setSid(null);
        }
        try {
            f.setCount(rs.getInt(su + "count"));
        } catch (SQLException e) {
            f.setCount(null);
        }
        try {
            f.setAdate(rs.getTimestamp(su + "adate"));
        } catch (SQLException e) {
            f.setAdate(null);
        }
        try {
            f.setTname(rs.getString(su + "tname"));
        } catch (SQLException e) {
            f.setTname(null);
        }
        try {
            f.setCid(rs.getLong(su + "cid"));
        } catch (SQLException e) {
            f.setCid(null);
        }
        try {
            f.setCname(rs.getString(su + "cname"));
        } catch (SQLException e) {
            f.setCname(null);
        }
        return f;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, AssignMessage f) {
        par.put("aid", f.getAid());
        par.put("tid", f.getTid());
        par.put("sid", f.getSid());
        par.put("count", f.getCount());
        par.put("adate", f.getAdate());
        par.put("tname", f.getTname());
        par.put("cid", f.getCid());
        par.put("cname", f.getCname());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            AssignMessage f) {
        args.put("aid", f.getAid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final AssignMessage f) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(f),
                        new String[] { "aid" });
                parsePS(ps, f);
                return ps;
            }

        };
    }
    
    private static String parseSQL(AssignMessage f) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_assign_message(");
        int count = 0;
        if (null != f.getTid()) {
            sql.append("tid,");
            count++;
        }
        if (null != f.getSid()) {
            sql.append("sid,");
            count++;
        }
        if (null != f.getCount()) {
            sql.append("count,");
            count++;
        }
        if (null != f.getAdate()) {
            sql.append("adate,");
            count++;
        }
        if (null != f.getCid()) {
            sql.append("cid,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }


    private static void parsePS(PreparedStatement ps, AssignMessage f)
        throws SQLException {
        int count = 1;
        if (null != f.getAid()) {
            ps.setLong(count++, f.getAid());
        }
        if (null != f.getTid()) {
            ps.setLong(count++, f.getTid());
        }
        if (null != f.getSid()) {
            ps.setLong(count++, f.getSid());
        }
        if (null != f.getCount()) {
            ps.setInt(count++, f.getCount());
        }
        if (null != f.getAdate()) {
            ps.setTimestamp(count++, f.getAdate());
        }
        if (null != f.getTname()) {
            ps.setString(count++, f.getTname());
        }
        if (null != f.getCid()) {
            ps.setLong(count++, f.getCid());
        }
        if (null != f.getCname()) {
            ps.setString(count++, f.getCname());
        }
    }

}
